<template>
  <div>
    <Card
      :title="copyFormData.pageTitle"
      :bordered="false"
      dis-hover
      class="ivu-mt"
    >
      <Form
        ref="formData"
        :model="formData"
        :rules="copyFormDataRules"
        :label-width="300"
      >
        <Row>
          <Col span="12">
            <h3>活动信息</h3>
          </Col>
        </Row>
        <Row>
          <Col
            span="18"
            offset="2"
            v-for="(item, index) in copyFormData"
            :key="index"
          >
            <component
              :itemData="item.data"
              :formData.sync="formData"
              v-bind:is="item.fileName"
              @handleChangeFormData="handleChangeFormData"
            ></component>
          </Col>
          <Col span="18" offset="2">
            <FormItem>
              <!-- <Button type="primary"
                                    @click="handleSubmit('formData')">提交</Button>
                            <Button @click="handleReset('formData')"
                                    class="ivu-ml-8">清空</Button> -->
            </FormItem>
          </Col>
        </Row>
        <Row>
          <div style="width: 100%; text-align: center">
            <Button
              type="primary"
              style="margin-right: 20px"
              :disabled="formData.issueState == 1"
              @click="updateIssueState(1)"
              >发布
            </Button>
            <Button
              v-if="!isCan"
              :disabled="formData.issueState == 2"
              @click="updateIssueState(2)"
              >取消发布</Button
            >
          </div>
        </Row>
      </Form>
    </Card>
    <Modal v-model="issueStateFlag" title="发布/取消发布" @on-ok="ok">
      <p>确定要执行操作吗？</p>
    </Modal>
  </div>
</template>
<script>
import { ApiRequest } from '@api/basePage';
import forms from '@/mixins/dynamicFormView'; // 混入通用代码
import qwSelectTree from '@/components/common/formItem/qwSelectTree'; // selectTree组件
import qwInput from '@/components/common/formItem/qwInput'; // input组件
import qwSelect from '@/components/common/formItem/qwSelect'; // select组件
import qwDatePicker from '@/components/common/formItem/qwDatePicker'; // 时间组件
import qwUpload from '@/components/common/formItem/qwUpload'; // upload组件
import qwCascader from '@/components/common/formItem/qwCascader'; // 级联组件
import qwRadio from '@/components/common/formItem/qwRadio'; // 单选组件
import qwCheckbox from '@/components/common/formItem/qwCheckbox'; // 多选组件
import qwQuill from '@/components/common/formItem/qwQuill'; // 多选组件

export default {
  mixins: [forms],
  components: {
    qwSelectTree,
    qwInput,
    qwDatePicker,
    qwSelect,
    qwUpload,
    qwCascader,
    qwRadio,
    qwCheckbox,
    qwQuill
  },
  data () {
    return {
      isCan: false,
      issueStateFlag: false,
      issueStateVal: ''
    };
  },
  methods: {
    // 修改发布状态
    updateIssueState (val) {
      this.issueStateFlag = true;
      this.issueStateVal = val;
    },
    ok () {
      let data = {};
      ApiRequest(
        '/sysback/cashback/updateIssueState?issueState=' +
          this.issueStateVal +
          '&uuid=' +
          this.uuid,
        'POST',
        data
      ).then(res => {
        if (res.retStatus === '1') {
          this.$Message.success('执行成功');
          this.getViewPageInfo();
        }
      });
    },

    getViewPageInfo () {
      let data = {
        // uuid: this.uuid
      };
      // 开始请求会员信息
      ApiRequest(
        this.requestApi.getViewPageInfo.url + '?uuid=' + this.uuid,
        this.requestApi.getViewPageInfo.type,
        data
      ).then(res => {
        let retData = res.retData;
        this.formData = retData;
        var endTime = new Date(this.formData.endTime).getTime();
        var nowTime = Date.now();
        if (endTime < nowTime) {
          this.isCan = true;
        }
      });
    },
    // 获取初始配置信息
    getConfigData () {
      this.pagePath = this.$route.path;
      let dynamicData = require('./configjs/returnActivityView').default;
      this.requestApi = dynamicData.requestApi; // 页面所需接口集合
      this.$set(this.$data, 'dynamicData', dynamicData);

      // copy 一份数据 方便清空表单数据
      let copyFormData = JSON.parse(JSON.stringify(this.dynamicData.formData));
      let copyFormDataRules = JSON.parse(
        JSON.stringify(this.dynamicData.formDataRules)
      );
      // 传给子组件的数据
      this.copyFormData = copyFormData;
      this.copyFormDataRules = copyFormDataRules;

      // 动态添加formData 提交数据内容
      copyFormData.forEach(item => {
        const obj = item.data;
        let name = '';
        let val = '';
        if (obj.type === 'inputRange' || obj.type === 'inputNumberRange') {
          obj.valueArr.forEach(item => {
            name = item.name;
            val = item.value;
            this.$set(this.formData, name, val);
          });
        } else {
          name = obj.name;
          val = obj.value;
          this.$set(this.formData, name, val);
        }
      });

      // 获取页面信息
      this.getViewPageInfo();
    }
  }
};
</script>
